var uuidV4 = require('uuid/v4');
var Eventproxy = require('eventproxy');
var logger = require('../common/logger');
var models = require('../models');
var tools = require('../common/tools');
var config = require('config-lite');
var Sequelize = require('sequelize');
var moment = require('moment');
var _ = require('lodash');

exports.indexPage = function indexPage(req, res) {
  return res.render('dashboard/index');
};

exports.infos = function infos(req, res, next) {
  var ep = new Eventproxy();
  ep.fail(next);

  var user = req.user;
  var reqQuery = req.query;

  var time = reqQuery.time || moment().format('YYYY-MM-DD');
  var tomorrow = moment(time, 'YYYY-MM-DD').add(1, 'days').format('YYYY-MM-DD');

  var condition = {
    where: {}
  };

  if (user.plevel !== config.plevels[0]) {
    condition.where.event_uuid = user.event_uuid;
  }

  ep.all('getTotal', 'getTodayDining', 'getTotalDining', function(total, todayDining, totalDining) {
    return res.send_data({
      // entities: {
      //   total: total,
      //   today_dining: todayDining,
      //   total_dining: totalDining
      // }
      entities: [
        {
          title: '总制证人数',
          value: total
        }, {
          title: '今日用餐人数',
          value: todayDining
        }, {
          title: '总用餐人数',
          value: totalDining
        }
      ]
    });
  });

  models.staff.findAll(condition).then(function(staffs) {
    ep.emit('getTotal', staffs.length);
  });

  condition.include = [{
    model: models.area,
    where: {
      name: {
        $like: '%餐厅%'
      }
    }
  }];
  models.signlog.findAll(condition).then(function(returnData) {
    ep.emit('getTotalDining', returnData.length);
  });
  condition.where.time = {};
  condition.where.time['$gte'] = time;
  condition.where.time['$lt'] = tomorrow;
  models.signlog.findAll(condition).then(function(returnData) {
    ep.emit('getTodayDining', returnData.length);
  });
};

exports.chart = function chart(req, res, next) {
  var ep = new Eventproxy();
  ep.fail(next);

  var user = req.user;
  var reqQuery = req.query;

  var time = reqQuery.time || 'all';
  if (user.plevel === config.plevels[0]) {
    time = 'all';
  }

  var tomorrow;
  if (time !== 'all') {
    tomorrow = moment(time, 'YYYY-MM-DD').add(1, 'days').format('YYYY-MM-DD');
  }

  ep.all('getData', function(dataArr) {
    if (user.plevel === config.plevels[0]) {
      var condition = {
        where: {},
        include: [{
          model: models.event
        }],
        attributes: ['*', [Sequelize.fn('COUNT', 'event_uuid'), 'count']],
        group: 'event_uuid'
      };
      if (tomorrow) {
        condition.where.time = {};
        condition.where.time['$gte'] = time;
        condition.where.time['$lt'] = tomorrow;
      }
      logger.debug(condition);
      models.staff.findAll(condition).then(function(returnData) {
        var events = returnData.map(function(event) {
          var _event = event.toJSON();
          return {
            uuid: _event.event.uuid,
            title: _event.event.name,
            value: _event.count
          };
        });
        var _events = _.assign(dataArr, events);
        return res.send_data({
          entities: _events
        });
      });
    } else {
      var condition = {
        where: {},
        include: [{
          model: models.area
        }],
        attributes: ['*', [Sequelize.fn('COUNT', 'area_uuid'), 'count']],
        group: 'area_uuid'
      };
      if (tomorrow) {
        condition.where.time = {};
        condition.where.time['$gte'] = time;
        condition.where.time['$lt'] = tomorrow;
      }
      logger.debug(condition);
      models.signlog.findAll(condition).then(function(returnData) {
        var areas = returnData.map(function(area) {
          var _area = area.toJSON();
          return {
            title: _area.area.name,
            value: _area.count
          };
        });
        var _areas = _.assign(dataArr, areas);
        return res.send_data({
          entities: _areas
        });
      });
    }
  });

  if (user.plevel === config.plevels[0]) {
    models.event.findAll().then(function(returnData) {
      var events = returnData.map(function(event) {
        var _event = event.toJSON();
        return {
          uuid: event.uuid,
          title: _event.name,
          value: 0
        };
      });
      ep.emit('getData', events);
    });
  } else {
    models.area.findAll({
      where: {
        event_uuid: user.event_uuid
      }
    }).then(function(returnData) {
      var areas = returnData.map(function(area) {
        var _area = area.toJSON();
        return {
          uuid: area.uuid,
          title: _area.name,
          value: 0
        };
      });
      ep.emit('getData', areas);
    });
  }
};
